package com.xyauto.theme.utils

import android.os.Build
import com.autochips.metazone.AtcMetazone

/**
 * created by gongziyi on 2023/12/8 11:46
 * AtcMetazone 工具相关
 */
object AtcMetaZoneTools {

    //界面易显示/隐藏 主题标志位
    const val KEY_DISPLAY_THEME_LIST = 0x323

    //选择主题类型
    const val KEY_SELECT_TYPE = 0x375

    //选择主题名称
    const val KEY_SELECT_THEME_NAME = 0x37a


    fun writereserved_offset(value: Any, key: Int, length: Int, flush: Boolean = false) {
        if (isQualcomm())return
        val bytes = bytesForm(value, length)
        AtcMetazone.writereserved_offset(bytes, key, length)
        if (flush) {
            AtcMetazone.flush(true)
        }
    }

    fun readreserved_offset(targetBytes: ByteArray, key: Int) {
        if (isQualcomm())return
        AtcMetazone.readreserved_offset(targetBytes, key, targetBytes.size)
    }


    private fun bytesForm(value: Any, length: Int) = ByteArray(length).apply {
        if (length <= 0) return@apply
        when {
            value is String -> {
                val values = value.toByteArray()
                for (i in values.indices) {
                    if (i < length) {
                        this[i] = values[i]
                    }
                }
            }
            value is Int -> this[0] = value.toByte()
            value is Boolean -> this[0] = if (value) {
                1
            } else {
                0
            }.toByte()
        }
    }

    fun isQualcomm(): Boolean = Build.HARDWARE?.let {
        it.contains("qcom") || Build.VERSION.SDK_INT >= 30
    } ?: false
}